import { queryNotices } from '@/services/api';

export default {
  namespace: 'global',

  state: {
    collapsed: false,
    amapObject: null,
    notices: [],
    rankList: [],
    brandList: [],
    buyTypeList: [],
    saleTypeList: [],
    unitList: [],
    productStatusList: [],
    orderTypeList: [],
    orderStatusList: [],
    payTypeList: [],
    consumerTypeList: [],
    cityList: [],
    genderList: [],
    salerStatusList: [],
    deliverRegionList: [],
    consumerLevelList: [],
    autonomousAuthorityList: [],
    transactionNoticeList: [],
    purchaseAuthorityList: [],
    shopDisplayList: [],
    shopFeeList: [],
    shopStatusList: [],
  },

  effects: {
    *setAMapObject({ payload }, { put }) {
      yield put({
        type: 'handleAmapObject',
        payload,
      });
    },
    *fetchNotices(_, { call, put }) {
      const data = yield call(queryNotices);
      yield put({
        type: 'saveNotices',
        payload: data,
      });
      yield put({
        type: 'user/changeNotifyCount',
        payload: data.length,
      });
    },
    *clearNotices({ payload }, { put, select }) {
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
      const count = yield select(state => state.global.notices.length);
      yield put({
        type: 'user/changeNotifyCount',
        payload: count,
      });
    },
    *setRank({ payload }, { put }) {
      yield put({
        type: 'changeRankList',
        payload,
      });
    },
    *setBrand({ payload }, { put }) {
      yield put({
        type: 'changeBrandList',
        payload,
      });
    },
    *setBuyType({ payload }, { put }) {
      yield put({
        type: 'changeBuyTypeList',
        payload,
      });
    },
    *setSaleType({ payload }, { put }) {
      yield put({
        type: 'changeSaleTypeList',
        payload,
      });
    },
    *setUnit({ payload }, { put }) {
      yield put({
        type: 'changeUnitList',
        payload,
      });
    },
    *setProductStatus({ payload }, { put }) {
      yield put({
        type: 'changeProductStatusList',
        payload,
      });
    },
    *setOrderType({ payload }, { put }) {
      yield put({
        type: 'changeOrderTypeList',
        payload,
      });
    },
    *setOrderStatus({ payload }, { put }) {
      yield put({
        type: 'changeOrderStatusList',
        payload,
      });
    },
    *setPayType({ payload }, { put }) {
      yield put({
        type: 'changePayTypeList',
        payload,
      });
    },
    *setConsumerType({ payload }, { put }) {
      yield put({
        type: 'changeConsumerTypeList',
        payload,
      });
    },
    *setCity({ payload }, { put }) {
      yield put({
        type: 'changeCityList',
        payload,
      });
    },
    *setGender({ payload }, { put }) {
      yield put({
        type: 'changeGenderList',
        payload,
      });
    },
    *setSalerStatus({ payload }, { put }) {
      yield put({
        type: 'changeSalerStatusList',
        payload,
      });
    },
    *setDeliverRegion({ payload }, { put }) {
      yield put({
        type: 'changeDeliverRegionList',
        payload,
      });
    },
    *setConsumerLevel({ payload }, { put }) {
      yield put({
        type: 'changeConsumerLevelList',
        payload,
      });
    },
    *setAutonomousAuthority({ payload }, { put }) {
      yield put({
        type: 'changeAutonomousAuthorityList',
        payload,
      });
    },
    *setTransactionNotice({ payload }, { put }) {
      yield put({
        type: 'changeTransactionNoticeList',
        payload,
      });
    },
    *setPurchaseAuthority({ payload }, { put }) {
      yield put({
        type: 'changePurchaseAuthorityList',
        payload,
      });
    },
    *setShopDisplay({ payload }, { put }) {
      yield put({
        type: 'changeShopDisplayList',
        payload,
      });
    },
    *setShopFee({ payload }, { put }) {
      yield put({
        type: 'changeShopFeeList',
        payload,
      });
    },
    *setShopStatus({ payload }, { put }) {
      yield put({
        type: 'changeShopStatusList',
        payload,
      });
    },
  },

  reducers: {
    handleAmapObject(state, { payload }) {
      return {
        ...state,
        amapObject: payload,
      };
    },
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state, { payload }) {
      return {
        ...state,
        notices: payload,
      };
    },
    saveClearedNotices(state, { payload }) {
      return {
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
    changeRankList(state, { payload }) {
      return {
        ...state,
        rankList: payload,
      };
    },
    changeBrandList(state, { payload }) {
      return {
        ...state,
        brandList: payload,
      };
    },
    changeBuyTypeList(state, { payload }) {
      return {
        ...state,
        buyTypeList: payload,
      };
    },
    changeSaleTypeList(state, { payload }) {
      return {
        ...state,
        saleTypeList: payload,
      };
    },
    changeUnitList(state, { payload }) {
      return {
        ...state,
        unitList: payload,
      };
    },
    changeProductStatusList(state, { payload }) {
      return {
        ...state,
        productStatusList: payload,
      };
    },
    changeOrderTypeList(state, { payload }) {
      return {
        ...state,
        orderTypeList: payload,
      };
    },
    changeOrderStatusList(state, { payload }) {
      return {
        ...state,
        orderStatusList: payload,
      };
    },
    changePayTypeList(state, { payload }) {
      return {
        ...state,
        payTypeList: payload,
      };
    },
    changeConsumerTypeList(state, { payload }) {
      return {
        ...state,
        consumerTypeList: payload,
      };
    },
    changeCityList(state, { payload }) {
      return {
        ...state,
        cityList: payload,
      };
    },
    changeGenderList(state, { payload }) {
      return {
        ...state,
        genderList: payload,
      };
    },
    changeSalerStatusList(state, { payload }) {
      return {
        ...state,
        salerStatusList: payload,
      };
    },
    changeDeliverRegionList(state, { payload }) {
      return {
        ...state,
        deliverRegionList: payload,
      };
    },
    changeConsumerLevelList(state, { payload }) {
      return {
        ...state,
        consumerLevelList: payload,
      };
    },
    changeAutonomousAuthorityList(state, { payload }) {
      return {
        ...state,
        autonomousAuthorityList: payload,
      };
    },
    changeTransactionNoticeList(state, { payload }) {
      return {
        ...state,
        transactionNoticeList: payload,
      };
    },
    changePurchaseAuthorityList(state, { payload }) {
      return {
        ...state,
        purchaseAuthorityList: payload,
      };
    },
    changeShopDisplayList(state, { payload }) {
      return {
        ...state,
        shopDisplayList: payload,
      };
    },
    changeShopFeeList(state, { payload }) {
      return {
        ...state,
        shopFeeList: payload,
      };
    },
    changeShopStatusList(state, { payload }) {
      return {
        ...state,
        shopStatusList: payload,
      };
    },
  },

  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
        if (typeof window.ga !== 'undefined') {
          window.ga('send', 'pageview', pathname + search);
        }
      });
    },
  },
};
